package zj.com.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zj.com.model.Dzg;
import zj.com.util.DateUtil;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/3.
 */
public class DshAdapter extends BaseAdapter{

    public List<Dzg> list;
    public Context context;
    public LayoutInflater inflater;

    public boolean isEdit = false;
    public Map<Integer,Boolean> selectMap = new HashMap<Integer,Boolean>();
    public int selectPosition = -1;

    public DshAdapter(List<Dzg> list, Context context){

        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_djc_item, null);
            viewHolder.type_tv = (TextView) convertView.findViewById(R.id.type_tv);
            viewHolder.cb = (CheckBox) convertView.findViewById(R.id.cb);
            viewHolder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
            viewHolder.des_tv = (TextView) convertView.findViewById(R.id.des_tv);
            viewHolder.jc_type_tv = (TextView) convertView.findViewById(R.id.jc_type_tv);
            viewHolder.date_tv = (TextView) convertView.findViewById(R.id.date_tv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Dzg dzg = list.get(position);
        viewHolder.name_tv.setText(dzg.getInspectTaskName());
        viewHolder.des_tv.setText(dzg.getInspectOrgName());
        viewHolder.jc_type_tv.setText(dzg.getInspectTypeName());
        String date = dzg.getInspectDate();
        viewHolder.date_tv.setText(DateUtil.formatStrimgDate2(date));
//        if(isEdit){
//            viewHolder.cb.setVisibility(View.VISIBLE);
//            viewHolder.type_tv.setVisibility(View.GONE);
//        }else{
//            viewHolder.type_tv.setVisibility(View.VISIBLE);
//            viewHolder.cb.setVisibility(View.GONE);
//        }

        String type = dzg.getInspectTypeName();
        if(type != null && type.length()>2){

            viewHolder.type_tv.setText(type.substring(0,2));
        }
        if(type.equals("日常检查"))
        {
            viewHolder.type_tv.setBackgroundResource(R.drawable.rc);
            viewHolder.type_tv.setText("日常");

        }else  if(type.equals("专项检查")){
            viewHolder.type_tv.setBackgroundResource(R.drawable.zx);
            viewHolder.type_tv.setText("专项");

        }else if(type.equals("综合检查"))
        {
            viewHolder.type_tv.setBackgroundResource(R.drawable.zh);
            viewHolder.type_tv.setText("综合");


        }else if(type.equals("其它"))
        {

            viewHolder.type_tv.setBackgroundResource(R.drawable.qt);
            viewHolder.type_tv.setText("其它");


        }
        else if(type.endsWith("节假日检查")){
            viewHolder.type_tv.setBackgroundResource(R.drawable.rc);
            viewHolder.type_tv.setText("节假");
        }

//
//        if(position % 2 == 0){
//            viewHolder.type_tv.setBackgroundResource(R.drawable.gl_icon);
//
//        }else{
//            viewHolder.type_tv.setBackgroundResource(R.drawable.jx_icon);
//        }
//
//        viewHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//
//                selectMap.put(position,isChecked);
//            }
//        });

        if(selectPosition == position){
            selectMap.put(position,true);
            viewHolder.cb.setChecked(true);
        }else{
            selectMap.remove(position);
            viewHolder.cb.setChecked(false);
        }



        return convertView;
    }
    private static class ViewHolder {
        TextView type_tv;
        TextView itemTitle;
        CheckBox cb;
        TextView name_tv;
        TextView des_tv;
        TextView jc_type_tv;
        TextView date_tv;


    }

    public void setIsEdit(boolean isEdit){

        this.isEdit = isEdit;
        notifyDataSetChanged();
    }

    public void setDataChange(List<Dzg> list){

        this.list = list;
        notifyDataSetChanged();
    }

    public void setSelect(int position){

        this.selectPosition = position;
        notifyDataSetChanged();
    }

    public Map<Integer,Boolean> getSelectMap(){

        return selectMap;
    };
}
